package com.shelpe.services.sinbad.controller.dto;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.controller.dto.vo.KeywordRanking;
import com.shelpe.services.sinbad.controller.dto.vo.RankingPremium;
import com.shelpe.services.sinbad.entity.dto.BaseDto;
import com.shelpe.services.sinbad.entity.jpa.Tactics;
import com.shelpe.services.sinbad.service.utils.EntityUtils;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "卡位")
public class RankItem extends BaseDto<Tactics> {
	public final static int DEVICE_PC = 2;
	public final static int DEVICE_MOBILE = 1;
	public final static String DEVICE_pc = "pc";
	public final static String DEVICE_mobile = "mobile";

	@JsonProperty("tactics_id")
	private long rankingID;
	@JsonProperty("tactics_name")
	private String name;

	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
	@JsonProperty("from")
	private Date fromDate; // 开始周期
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
	@JsonProperty("to")
	private Date toDate;// 结束周期
	@JsonProperty("campaign_period")
	private int campaignPeriod;// 频率
	@JsonProperty("day_period")
	private String dayPeriod;// 天频率
	@JsonProperty("schedule_period")
	private Tactics.SchedulePeriod schedulePeriod;// 时间频率
	@JsonProperty("heighest_price")
	private int heighestPrice;

	private String device;
	@JsonProperty("tactics_status")
	private String status;// open, pause, finish;

	private RankingPremium premium;
	@JsonProperty("keywords")
	private List<KeywordRanking> keywords;

	@Override
	public void from(Tactics po) {
		BeanUtils.copyProperties(po, this);
		this.setRankingID(po.getTacticsID());
		if (DEVICE_MOBILE == po.getDevice()) {
			this.setDevice(DEVICE_mobile);
		} else {
			this.setDevice(DEVICE_pc);
		}
		if (po.getStatus() == Tactics.Status.finish) {
			this.setStatus("finish");
		} else if (po.getStatus() == Tactics.Status.pause) {
			this.setStatus("pause");
		} else {
			this.setStatus("open");
		}
		if (po.getFromDate() != null) {
			this.setFromDate(po.getFromDate());
		}
		if (po.getToDate() != null) {
			this.setToDate(po.getToDate());
		}
		if (StringUtils.isEmpty(po.getSchedulePeriod())) {
			po.setSchedulePeriod("00:00-23:59");
		}
		this.setSchedulePeriod(EntityUtils.toSchedulePeriod(po.getSchedulePeriod()));
		if (po.getPremium() != null) {
			this.setPremium(new RankingPremium());
			this.getPremium().setValue(po.getPremium().getValue());
			if (po.getPremium().isPercent()) {
				this.getPremium().setType(0);
			} else {
				this.getPremium().setType(1);
			}
		}
	}

	@Override
	public Tactics to() {
		Tactics tactics = new Tactics();
		this.toPo(tactics);
		return tactics;
	}

	public void to(Tactics tactics) {
		this.toPo(tactics);
	}

	public void toPo(Tactics tactics) {
		if (StringUtils.isNotBlank(this.name)) {
			tactics.setName(this.name);
		}
		if (this.heighestPrice > 0) {
			tactics.setHeighestPrice(this.heighestPrice);
		}
		if (StringUtils.isNotBlank(this.device)) {
			if (DEVICE_pc.equals(this.device)) {
				tactics.setDevice(DEVICE_PC);
			} else {
				tactics.setDevice(DEVICE_MOBILE);
			}
		}
		if (StringUtils.isNotBlank(this.status)) {
			if ("open".equals(this.status)) {
				tactics.setStatus(Tactics.Status.starting);
			} else if ("pause".equals(this.status)) {
				tactics.setStatus(Tactics.Status.pause);
			} else if ("finish".equals(this.status)) {
				tactics.setStatus(Tactics.Status.finish);
			}
		}
		if (this.getSchedulePeriod() != null) {
			tactics.setSchedulePeriod(EntityUtils.fromSchedulePeriod(this.getSchedulePeriod()));
		}
		if (this.getPremium() != null && this.getPremium().getValue() > 0.0f) {
			tactics.setPremium(this.getPremium().to());
		}
		if (this.campaignPeriod > 0) {
			tactics.setCampaignPeriod(this.campaignPeriod);
			tactics.setNextTime(DateUtils.addSeconds(new Date(), this.campaignPeriod));
		}
		if (StringUtils.isNotBlank(this.dayPeriod)) {
			tactics.setDayPeriod(this.dayPeriod);
		}
		if (this.fromDate != null) {
			tactics.setFromDate(this.fromDate);
		}
		if (this.toDate != null) {
			tactics.setToDate(this.toDate);
		}
	}
}
